package com.flower.salary.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.flower.salary.common.utils.JwtUtil;
import com.flower.salary.common.utils.LocalUser;
import com.flower.salary.domain.entity.SysMenu;
import com.flower.salary.domain.vo.Ztree;
import com.flower.salary.mapper.SysMenuMapper;
import com.flower.salary.mapper.SysRoleMenuMapper;
import com.flower.salary.mapper.SysUserRoleMapper;
import com.flower.salary.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lavender
 * @version 1.0
 * @date 2023-12-22 16:42
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {


    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;



    @Override
    public List<Ztree> getRoutes(String token) {
        // 获取用户id
       // int userId = JwtUtil.getUserId(token);
        // 获取用户角色
        //SysUserRole sysUserRole = sysUserRoleMapper.selectById(userId);
        //System.out.println(sysUserRole);
        // 根据用户角色查询用户菜单
        List<SysMenu> sysMenus = sysMenuMapper.selectByRoleId(2);




        return tree(sysMenus);
    }

    @Override
    public String getRole(String token) {

        int userId = JwtUtil.getUserId(token);

        if(userId==1){
            return "admin";
        }
        return "common";
    }


    private List<Ztree> tree(List<SysMenu> menus){

        //查询出所有的菜单
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (SysMenu menu : menus){

            Ztree ztree = new Ztree();
            ztree.setId(menu.getMenuId());
            ztree.setPId(menu.getParentId());
            ztree.setTitle(menu.getMenuName());
            ztrees.add(ztree);
        }


        //根节点存储
        List<Ztree> rootMenu = new ArrayList<>();

        //根据传递的参数设置根节点 父节点是0的，为根节点
        for (Ztree nav : ztrees) {
            if(nav.getPId() == 0){
                rootMenu.add(nav);
            }
        }

        //为根节点设置子菜单，getChild是递归调用
        for (Ztree nv : rootMenu) {
            //获取根节点下的所有子节点，使用getChild方法
            List<Ztree> childList = getChild(nv.getId(),ztrees);
            //给根节点设置子节点
            nv.setChildren(childList);
        }


        return rootMenu;
    }



    // 递归获取子菜单
    private List<Ztree> getChild(Long id, List<Ztree> allMenu) {
        //子菜单
        List<Ztree> childList = new ArrayList<>();
        for (Ztree nav : allMenu) {
            //遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
            //相等说明：为该根节点的子节点
            if(nav.getPId().equals(id)){
                childList.add(nav);
            }
        }
        //递归设置子节点
        for (Ztree nav : childList) {
            nav.setChildren(getChild(nav.getId(),allMenu));
        }

        //如果节点下没有子节点，返回一个空List（递归退出）
        if(childList.size() == 0){
            return new ArrayList<Ztree>();
        }
        return childList;
    }

}
